package com.test.demo.algorithm.datastructure.linkedlist;

/**
 * 链表反转
 * 输入：1->2->3
 * 输出：3->2->1
 */
public class LinkedlistReversal {
    public static void main(String[] args) {
        LinkedlistNode linkedlistNode = new LinkedlistNode(1, new LinkedlistNode(2, new LinkedlistNode(3)));
        System.out.println(linkedlistNode);
        linkedlistReversal(linkedlistNode);
    }

    /**
     * 遍历解决
     * @param linkedlistNode
     */
    public static void linkedlistReversal(LinkedlistNode linkedlistNode) {
        //当前节点
        LinkedlistNode currentNode = linkedlistNode;
        //下一个节点
        LinkedlistNode next;
        //上一个节点
        LinkedlistNode pre = null;
        while (currentNode != null) {
            next = currentNode.next;
            currentNode.next = pre;
            pre = currentNode;
            currentNode = next;
        }
        System.out.println(pre);
    }
}
